package com.ibiaoke.boss.common.intercepter;

import com.ibiaoke.boss.service.LoginService;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.consts.WeChatConsts;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.customer.dao.ThirdPartLoginDao;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.ThirdPartLogin;
import com.ibiaoke.customer.service.ThirdPartLoginService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by huhaichao on 2017/6/6.
 */
public class LoginInterceptor implements HandlerInterceptor {

    String[] passUrls = {
            /*登录*/
            "/login.do"
            /*获取门店列表*/
            , "/listShop.do"
            /*获取附近门店*/
            , "/getNearestShop.do"
            /*楼层*/
            , "/listFloor.do"
            /*商品分类*/
            , "/listGoodsClass.do"
            , "/listGoods.do"
            , "/getGoods.do"
            /*退出登录*/
            , "/logout.do"
            , "/isLogin.do"
            /*手机号码登录*/
            , "/registerByMobile.do"
            , "/sendVerifyCode.do"
            , "/userVerify.do"
            , "/successRedirect.do"
            , "/createConfig.do"
            , "/index.do"
            , "/loginByMobileVerify"
            , "/weixinBind.do"
            , "/listAdvert.do"
            , "/listRush.do"
            , "/listValidRush.do"
            /*获取APP版本号*/
            , "/getAppVersion.do"
            , "/listTopicPage.do"
            , "/getTopicPage.do"
            /*忘记密码*/
            , "/forgetLoginPassword.do"
            /*微信后台回调*/
            , "/wxPayNotify"
            /*支付宝后台回调*/
            , "/aliPayNotify"
            /*支付宝前台回调*/
            , "/aliPayReturnUrl.do"
            /*省市区查询*/
            , "/listProvince.do"
            , "/getShoppingCartNum.do"
            ,"/getOrderWithCode.do"
            /*获取店铺订单*/
            ,"/listShopOrder.do"
            ,"/getCode.do"
            ,"/sendMobileVerifyCode.do"
            ,"/getRandomString.do"
            /*获取拼团商品*/
            ,"/listPtGoods.do"
            /*获取拼团商品详情*/
            ,"/getPtGoodsById"
            ,"/payForPtGoodsOpen"
            ,"/payForPtGoodsFlight"
            ,"/PtGoodsConfirm"
            ,"/PtGoodsDelete"
            ,"/listOpenGroup"
            ,"/goOpenGroup"
            ,"/getPicCode"
            ,"/payForPtGoods"
            ,"/listPtGoods"
            ,"/checkPtCount.do"
            ,"/listShoppingCart.do"
            ,"newSaveShoppingCart.do"
            ,"/newDeleteShopppingCart.do"
            ,"/newListShoppingCart.do"
            ,"/newSubmitShoppingCartOrder.do"
            ,"/xcxLogin.do"
    };
    @Autowired
    ThirdPartLoginService thirdPartLoginService;

    @Autowired
    ThirdPartLoginDao thirdPartLogindao;

    @Autowired
    LoginService loginService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            return true;
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        //不拦截
        for (String passUrl : passUrls) {
            if (request.getRequestURI().indexOf(passUrl) >= 0) {
                return true;
            }
        }

        /*Customer user1 = new Customer();
        user1.setCustomerId(69123);
        user1.setCustomerMobile("17372200823");
        request.getSession().setAttribute(CommonConsts.LOGIN_SESSION_NAME,user1);
        request.getSession().setAttribute(WeChatConsts.OPEN_ID,"o6PwK1pw3RLCnHpNn9LSs7Sz2iXU");
        request.getSession().setAttribute(WeChatConsts.Xcx_Id,"oz67u0NqAeVDOVBPrIPSbuqxTedQ");
        request.getSession().setAttribute("unionid","oC17ywVhstPjn60Sv0X7bPVwb01c");*/

        Customer  user= (Customer) request.getSession().getAttribute(CommonConsts.LOGIN_SESSION_NAME);
        String openid = (String) request.getSession().getAttribute(WeChatConsts.OPEN_ID);
        String unionid = (String) request.getSession().getAttribute("unionid");
        if (user == null) {
            //APP token登录
            /*String token = request.getParameter(CommonConsts.TOKEN);
            if (StringUtils.isNotEmpty(token)) {
                Customer customer = loginService.loginByToken(token);
                if (customer != null) {
                    request.getSession().setAttribute(CommonConsts.LOGIN_SESSION_NAME, customer);
                    return true;
                }
            }*/
            //微信openid登录
            if (StringUtils.isNotBlank(unionid)) {
                ThirdPartLogin thirdPartLogin = thirdPartLogindao.getByUnid(unionid);
                if (thirdPartLogin == null) {
                    Res.createErrorRes("new_user").write(response);
                }
            } else {
                Res.createErrorRes("not_login").write(response);
            }
            return false;
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
